<?php

	class Controller_Genres extends Controller_Base
	{
		function index()
		{
			$this->assignData('genres', WebApp::app()->db->select(array('*'),array('genres')));
		}
		function view()
		{
			$id_genre =  WebApp::app()->db->select(array('id'), array('genres'), "`name`='".$this->args[0]."'");
			$id_genre = $id_genre[0]->id;
			$rez = WebApp::app()->db->select(
					array('games.id', 'games.name'),
					array('genres','games','genres_games'),
					"genres.id = ".$id_genre." and genres.id = genres_games.id_genre and genres_games.id_game = games.id");
			$this->assignData('games_cat', $rez);
		}
		function NewAdd()
		{
			
			if	(	
					isset($_POST['btnAdd']) and 
					isset($_POST['g_name']) and 
					isset($_POST['g_descr']) and
					isset($_FILES['g_icon']['name'])
				)
			{				
				$file = $_FILES['g_icon'];
				WebApp::app()->db->insert	("genres", array(	"name"=>$_POST['g_name'],
																"descr"=>$_POST['g_descr'],
																"icon"=>$_FILES['g_icon']['name']
															)
											);
				WebApp::app()->image_resize($file['tmp_name'], "../images/galery/cat_icon/mini/".$file['name'],156,125);
				copy ($file['tmp_name'], "../images/galery/cat_icon/".$file['name']);
				header("Location:".$this->baseUrl."successAdd");
			}
		}
		function successAdd()
		{
			
		}
		function delete()
		{
			
			if(isset($_POST['ajax']))
			{
				$id_genres = $_POST['id_genres'];
				
				$id_games = WebApp::app()->db->select(array("id_game"), array("genres_games"), "id_genre = " . $id_genres);
				
				foreach($id_games as $id_game)
				{
					$icon = WebApp::app()->db->select(array("icon"), array("games"), "id = " . $id_game->id_game);
					unlink("../images/galery/game_icon/mini/" . $icon[0]->icon);
					unlink("../images/galery/game_icon/" . $icon[0]->icon);
					WebApp::app()->db->delete("games", "id = " . $id_game->id_game);
				}
				
				WebApp::app()->db->delete("genres_games", "id_genre = " . $id_genres);
				
				$icon = WebApp::app()->db->select(array("icon"), array("genres"), "id = " . $id_genres);
				unlink("../images/galery/cat_icon/mini/" . $icon[0]->icon);
				unlink("../images/galery/cat_icon/" . $icon[0]->icon);
				
				WebApp::app()->db->delete("genres", "id = " . $id_genres);
				
				echo true;
				exit;
			}
			
			
			//$this->assignData('baseURL', WebApp::app()->getBaseUrl());
			$this->assignData('genres', WebApp::app()->db->select(array('*'),array('genres')));
			$this->assignData('path', WebApp::app()->getBaseUrl());
			
		}
	}

?>